package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    /**
     * 科室删除
     */
    @RequestMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        //获取传过来的值
        Map<String, String[]> requestMap = request.getParameterMap();//获取所有的参数
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);//转换成map
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //获取科室编号
        String depcode = (String) paramMap.get("depcode");

        String hospSign = (String) paramMap.get("sign");
        //2.根据传过来的医院编号，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.将签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //4.比较签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    /**
     * 查询科室信息
     */
    @RequestMapping("/department/list")
    public Result findDepartment(HttpServletRequest request) {
        //获取传过来的值
        Map<String, String[]> requestMap = request.getParameterMap();//获取所有的参数
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);//转换成map
        //System.out.println("requestMap--------"+requestMap);
       // System.out.println("paramMap--------"+paramMap);
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //获取key
        String hospSign = (String) paramMap.get("sign");
        //获取
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        //当前页
//        if (paramMap.get("page") == null) {
//            int page = (int) paramMap.put("page", 1);
//        }
//        if (paramMap.get("limit") == null) {
//            int limit = (int) paramMap.put("size", 2);
//        }

        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));

        //签名校验
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
      }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> pageInfo = departmentService.selectPage(page, limit, departmentQueryVo);

        return Result.ok(pageInfo);
    }

    /**
     * 上传科室接口
     */
    @RequestMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //获取传过来的值
        Map<String, String[]> requestMap = request.getParameterMap();//获取所有的参数
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);//转换成map

        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //1.获取医院系统传过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传过来的医院编号，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.将签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //4.比较签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(paramMap);

        return Result.ok();
    }


    /**
     * 根据医院id查询医院信息
     */
    @RequestMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取传过来的值
        Map<String, String[]> requestMap = request.getParameterMap();//获取所有的参数
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);//转换成map

        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //1.获取医院系统传过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传过来的医院编号，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.将签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //4.比较签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service方法，查询医院信息
        Hospital hospital = hospitalService.getByHospital(hoscode);
        return Result.ok(hospital);
    }

    /**
     * 上传医院接口
     */
    @RequestMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        //获取传过来的值
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //1.获取医院系统传过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传过来的医院编号，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.将签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //4.比较签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中“+” 转成了“ ”，转换回来
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);

        //调用service
        hospitalService.save(paramMap);
        return Result.ok();
    }

    Map<String, Object> getValue(HttpServletRequest request) {

        //获取传过来的值
        Map<String, String[]> requestMap = request.getParameterMap();//获取所有的参数
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);//转换成map

        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //1.获取医院系统传过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传过来的医院编号，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.将签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //4.比较签名是否一致
        if (!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        return paramMap;
    }


}
